package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzg.common.demo.country.ProvinceCityAndCounty;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.entity.ChinaProvince;
import com.fzg.mapper.ChinaProvinceMapper;
import com.fzg.service.ChinaProvinceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 中国的所有省份 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-11-18
 */
@Slf4j
@Service
public class ChinaProvinceServiceImpl extends ServiceImpl<ChinaProvinceMapper, ChinaProvince> implements ChinaProvinceService {


    @Override
    public ResponseWrapper importProvinceDataToMySQL() {

        // 获取数据(已经把文本数据转换成list了)
        ArrayList<HashMap<String, Object>> list = ProvinceCityAndCounty.getChineseAllProvincesAndCityCounty("E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\country\\province.txt");
        if (list != null){
            // 直接存储到MySQL
            int insertCount = 0;
            int updateCount = 0;
            for (HashMap<String, Object> map : list) {
                Object code = map.get("code");
                Object parentCode = map.get("parentCode");
                Object chineseName = map.get("chineseName");
                Object englishName = map.get("englishName");
                List<ChinaProvince> lists = this.list(new LambdaQueryWrapper<ChinaProvince>()
                        .eq(ChinaProvince::getProvinceName, chineseName)
                        .eq(ChinaProvince::getEnglishName, englishName)
                );
                // 如果有了，就更新code
                if (lists.size() > 0){
                    boolean update = this.update(new LambdaUpdateWrapper<ChinaProvince>()
                            .eq(ChinaProvince::getProvinceName, chineseName)
                            .eq(ChinaProvince::getEnglishName, englishName)
                            .set(ChinaProvince::getCode, code)
                            .set(ChinaProvince::getParentCode,parentCode)
                    );
                    if (update){
                        updateCount++;
                    }
                }else {  // 没有就插入
                    boolean save = this.save(new ChinaProvince() {{
                        setProvinceName(chineseName.toString());
                        setEnglishName(englishName.toString());
                        setCode(code.toString());
                        setParentCode(parentCode.toString());
                    }});
                    if (save){
                        insertCount++;
                    }
                }
            }
            return ResponseWrapper.markCustomSuccess("插入了" + insertCount + "条数据;\n 更新了" + updateCount + "条数据");
        }else {
            return ResponseWrapper.markCustomError("数据为空");
        }
    }
}
